package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
 * @author Lucie
 *class Add_New_User_Controler
 *Class variables:msg,flag
 *Class methods:add_User,check_input,String get_msg,set_msg
 */
public class Add_New_User_Controler {
	private String msg; 
	private boolean flag;
	/**
	 * Constructor initializes the class variables
	 */
	public Add_New_User_Controler()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	
	/**
	 * 
	 * @return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	 * This method chain messages into a string ,
	 * the purpose ultimately is to display on the screen all the failures
	 * @param new_msg
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}

	/**
	 * 
	 * @param user_id
	 * @param user_email
	 * @param user_role
	  * this  method call the class ;serverproxy, to Contact with the server,
  * first we check the Parameters we get (check_input) if something is wrong we return false 
	 *than we continue and check that we are not trying to add the user connected if we are we return false
	 *than we call the method getUserdetails that check if the user already exist and if he thus exist we return false
	 *and set an error msg  if everything sucsed  we add the user to the db (addUser)if something go wrong we return false 
	 * @return  boolean:If the calls in the method failed it returns false otherwise returns true
	 */
	 public boolean add_User(String user_id,String user_email,String user_role)
		{
		 ArrayList<String> str_check=new  ArrayList<String>();
		 	ArrayList<String> str_user=new  ArrayList<String>();
	 
		 check_input( user_id,  user_email,user_role);
		 	if (flag==false)
		 return false;
		 	if(user_id.equals(new Integer(BusinessImplements.GetInstance().getUserLogin().getUserID()).toString()))
		 	{
		 		set_msg("\nerror\nyou are tring to add the user you log in with "); 
		 		return false;
		 	}
		 	str_check=BusinessImplements.GetInstance().getServerProxy().getUserdetails(user_id);
		 	if(!(str_check.get(0)==null))
		 	{
		 		set_msg("\nuser all ready exist"); 
		 		return false;
		 	}
		 
		 	
			 	str_user=BusinessImplements.GetInstance().getServerProxy().addUser( user_id,  user_email,"1234",user_role);
				
				 	if (str_user.get(0).equals("false"))
				 	{
				 		set_msg("\nadd failed\nPlease try again"); 
				 		return false;
				 	}
			 	
			 	
		 	return flag;
 	
}
/**
 * 
 * @param id
 * @param email
 * @param role
 *  This method checks the parameters we have received are not empty
 * if they are poses flag= false;
 */
		public void check_input(String id, String email,String role)
		{
			
			if(id.equals(""))
			{
				set_msg("\nmising user id ");
				flag=false;
			}
			if(email.equals(""))
			{
				set_msg("\nmising user email ");
				flag=false;
			}
			if(role.equals(""))
			{
				set_msg("\nmising user role");
				flag=false;
			}
		
			
		}
	
}